<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL & ~(E_NOTICE|E_WARNING ));
class User extends MY_Front_Controller {
	public function __construct() {
		parent::__construct();
		
		$this->load->library('SimpleLoginSecure');
		$this->simpleloginsecure->setUserTable('fe_users');
		$this->simpleloginsecure->setIsAdmin(0);
		$this->load->model('fe_user_model');
	}

	public function login() {	
        $this->template->write_view ( 'content', 'front/user/login.php');
        echo $this->template->render ('content', TRUE);
		die;

	}

	public function auth()
	{
		$response = array (
			'status' 	=> 1,
			'message'	=> ''
		);
		if(!$this->simpleloginsecure->login(htmlentities($this->input->post('email')), htmlentities($this->input->post('password')))) {
			$response = array (
				'status' 	=> 0,
				'message'	=> '<strong>' . lang('error') . ':</strong> ' . lang('frontend_login_error') . '.'
			);
		}
		echo json_encode($response);
		die;
	}

	public function create() {	
	    $this->load->helper('form');
        $this->load->library ( 'form_validation' );
        $this->form_validation->set_rules ( 'first_name', lang('first_name'), 'required' );
        $this->form_validation->set_rules ( 'last_name', lang('last_name'), 'required' );
        $this->form_validation->set_rules ( 'email', lang('email'), 'required|valid_email|is_unique[fe_users.user_email]' );
        $this->form_validation->set_rules ( 'password', lang('password'), 'required' );
        $this->form_validation->set_rules ( 'repassword', lang('repassword'), 'required|matches[password]' );

        $this->template->add_js('application/assets/js/register.js');
            	
        $data = array();
        if ($this->form_validation->run () == FALSE) {
        	if($this->session->userdata('selected_country') === FALSE){
        		$this->session->set_userdata(array('selected_country' => $this->config->item('def_country')));
        	}
        	 
        	if($this->session->userdata('selected_currency') === FALSE){
        		$this->session->set_userdata(array('selected_currency' => $this->config->item('def_currency')));
        	}
        	    	 
        	$this->load->model('product_model');
        	$this->load->model('category_model');
        	 
        	$products_promo = $this->product_model->getAll(array('promotion' => 1, 'language_id' => $this->config->item('lang_obj')->id))->result();
        	 
        	$data['products_promo'] = $products_promo;
        	$data['content'] 	= 'product/main';
        	 
        	$data['categories'] = $this->category_model->getAll(array('parent_id' => 0,'language_id' => $this->config->item('lang_obj')->id))->result();
        	$data['metals'] = explode(',',$this->config->item('metals_for_filter'));
        	$data['shapes'] = explode(';',$this->config->item('shape_filter'));
        	$data['sizes'] = explode(';',$this->config->item('carat_filter'));
        	$data['qualities'] = explode(';',$this->config->item('quality_filter'));
        	$data['settings'] = explode(';',$this->config->item('setting_filter'));
        	$data['colours'] = explode(';',$this->config->item('color_filter'));
        	
        	$this->template->add_js('application/assets/js/jqueryPlugins/jCarousel/lib/jquery.jcarousel.js');
        	$this->template->add_css('application/assets/js/jqueryPlugins/jCarousel/skins/tango/skin.css');
        	$this->template->add_js('application/assets/js/jqueryPlugins/nivoSlider/jquery.nivo.slider.js');
        	$this->template->add_css('application/assets/js/jqueryPlugins/nivoSlider/nivo-slider.css');
        	
        	$this->template->write_view ( 'content', 'front/user/create.php', $data );
	        $this->template->render ();
        }
        else {
			$hasher = new PasswordHash(8, false);
			$this->fe_user_model->fields['user_email'] = htmlentities($this->input->post('email'));
			$this->fe_user_model->fields['user_pass'] = $hasher->HashPassword($this->input->post('password'));
			$this->fe_user_model->fields['user_date'] = date('Y-m-m h:i:s');
			$this->fe_user_model->fields['first_name'] = htmlentities($this->input->post('first_name'));
			$this->fe_user_model->fields['last_name'] = htmlentities($this->input->post('last_name'));
			$user = $this->fe_user_model->fields_to_object();
			$user->insert();
			
			$this->simpleloginsecure->login($user->user_email, htmlentities($this->input->post('password')));

			redirect(site_url('user/my_account/'));

        }		
	}

	public function my_account(){
	    $this->load->helper('form');
        $this->load->library ( 'form_validation' );
        if (!$this->access->getUserId()){
        	redirect(site_url('user/create/'));
        }
        $userObj = $this->fe_user_model->get_by_id($this->access->getUserId());
        
        $this->form_validation->set_rules ( 'first_name', lang('first_name'), 'required' );
        $this->form_validation->set_rules ( 'last_name', lang('last_name'), 'required' );
        $email = ($this->input->post('email')) ? $this->input->post('email') : '';
        if ((empty($email)) || $email != $userObj->user_email) {
        	$this->form_validation->set_rules ( 'email', lang('email'), 'required|valid_email|is_unique[fe_users.user_email]' );
        }
        
        $password = ($this->input->post('current_pass')) ? $this->input->post('current_pass') : '';
        if (!empty($password)) {
        	$hasher = new PasswordHash(8, false);

	        $this->form_validation->set_rules ( 'password', lang('password'), 'required' );
	        $this->form_validation->set_rules ( 'repassword', lang('repassword'), 'required|matches[password]' );
	        if (!$hasher->CheckPassword($password, $userObj->user_pass)) {
	        	$this->form_validation->set_rules ( 'current_pass', lang('current_pass'), 'required|matches[current_pass_value]' );
	        }
	        else {
	        	$userObj->user_pass = $hasher->HashPassword($this->input->post('password'));	
	        }
        }
        
        $this->template->add_js('application/assets/js/register.js');
   	
        $data = array();
        if ($this->form_validation->run () == FALSE) {
	        $this->template->write_view ( 'content', 'front/user/my_account.php', array('user' => $userObj));
	        $this->template->render ();
        }
        else {
			$userObj->first_name = 	htmlentities($this->input->post('first_name'));
			$userObj->last_name = 	htmlentities($this->input->post('last_name'));
			$userObj->user_email = 	htmlentities($this->input->post('email'));
			
			$userObj->update();
			
			redirect(site_url('user/my_account/'));
        }			
	}
	
	public function my_address(){
		$this->load->helper('form');
        $this->load->library ( 'form_validation' );
		if (!$this->access->getUserId()){
        	redirect(site_url('user/create/'));
        }
        $userObj = $this->fe_user_model->get_by_id($this->access->getUserId());
        
        $this->form_validation->set_rules ( 'first_name', lang('first_name'), 'required' );
        $this->form_validation->set_rules ( 'last_name', lang('last_name'), 'required' );
        $this->form_validation->set_rules ( 'tel', lang('telephone'), 'required' );
        $this->form_validation->set_rules ( 'street', lang('street'), 'required' );
        $this->form_validation->set_rules ( 'zip', lang('zip'), 'required' );
        $this->form_validation->set_rules ( 'city', lang('city'), 'required' );
        $this->form_validation->set_rules ( 'country_id', lang('country'), 'required' );
        $company = htmlentities($this->input->post('company'));
        if (!empty($company)) {
        	$this->form_validation->set_rules ( 'tax_number', lang('tax_number'), 'required' );	
        }
        
             
        $this->template->add_js('application/assets/js/register.js');
   	
        $data = array();
        if ($this->form_validation->run () == FALSE) {
	        $this->template->write_view ( 'content', 'front/user/my_address.php', array('user' => $userObj));
	        $this->template->render ();
        }
        else {
			$userObj->first_name 	= 	htmlentities($this->input->post('first_name'));
			$userObj->last_name 	= 	htmlentities($this->input->post('last_name'));
			$userObj->company 		= 	htmlentities($this->input->post('company'));
			$userObj->tax_number	= 	htmlentities($this->input->post('tax_number'));
			$userObj->tel 			= 	htmlentities($this->input->post('tel'));
			$userObj->fax 			= 	htmlentities($this->input->post('fax'));
			$userObj->street 		= 	htmlentities($this->input->post('street'));
			$userObj->zip 			= 	htmlentities($this->input->post('zip'));
			$userObj->city 			= 	htmlentities($this->input->post('city'));
			$userObj->country_id 	= 	htmlentities($this->input->post('country_id'));
			$userObj->user_modified	= date('Y-m-m h:i:s');
			
			
			$userObj->update();
				
			redirect(site_url('user/my_address/'));
        }		
	}	
	
	public function my_orders($orderId = ''){
		if (!$this->access->getUserId()){
        	redirect(site_url('user/create/'));
        }
		if (!empty($orderId)) {
			$this->load->model('orders_model');
			$this->load->model('order_address_model');
			$this->load->model('country_model');
			$this->load->model('curier_model');
			$user = $this->session->userdata('user');
			$order = $this->orders_model->get_by_id($orderId);
			
			$data = array();
			$data['order'] = $order;
			$data['shipping'] = $this->order_address_model->get_by_id($order->shipping_id);
			$data['shipping_country'] = $this->country_model->get_by_id($data['shipping']->country_id);
			$data['billing'] = $this->order_address_model->get_by_id($order->billing_id);
			$data['billing_country'] = $this->country_model->get_by_id($data['billing']->country_id);
			$data['curier'] = $this->curier_model->get_by_id($order->shipping_method);
			
			
			$this->template->add_js('application/assets/js/shadowbox.js');
			$this->template->add_css('application/assets/css/shadowbox.css');
		
			$this->template->write_view ( 'content', 'front/order_details.php' , $data);
			$this->template->render ();
		} else {      
	        $this->template->write_view ( 'content', 'front/user/my_orders.php');
		    $this->template->render ();
		}
	}	
	
	public function jqgrid_show() {
		
		$this->load->model('orders_model');
		$this->load->model('order_address_model');
		$this->load->model('country_model');
		$user = $this->session->userdata('user');
		$this->db->cache_off();
		$orders = $this->orders_model->get_by_param(array('user_id' => $user['id'] ));
		$this->db->cache_on();
		
		$page = $_REQUEST['page']; // get the requested page
		$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
		$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
		if($totalrows) {
			$limit = $totalrows;
		}
		$total_rows = $orders;
		
		$count = count($total_rows);
		$response = new stdClass();
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages){
			$page = $total_pages;
		}
		if ($limit <= 0){
			$limit = 0;
		}
		$start = $limit * $page - $limit;
		if ($start<=0){
			$start = 0;
		}
		$response->page = $page;
		$response->total = $total_pages;
		$response->records = $count;
		$response->start = $start;
		$response->limit = $limit;
		
		$eligible_rows = array_slice($total_rows, $start, $limit);
		
		$i = 0;
		foreach($eligible_rows as $row) {
			$status = array("1"=>lang('processing'),
							"2"=>lang('producing') ,
							"3"=>lang('delivered')
					);
			$shiping = $this->order_address_model->get_by_id($row->shipping_id);
			$shiping_country = $this->country_model->get_by_id($shiping->country_id);
			
		
			$allProd = json_decode($row->products);
			$reorderQuerySreing = '';
			$e= 0;
			foreach ($allProd->content as $prod ){
				if  ($e != 0) $reorderQuerySreing .= '#';
				$reorderQuerySreing .= $prod->id .'_'. $prod->options->ring_size ;
			}
			$acction ='<a href="JavaScript:addOrderComment('.$row->id.')" title="'.lang('add_your_opiniom').'">'.lang('add_your_opiniom').'</a> ';
			$acction .= '<a href="'.base_url('user/my_orders/'. $row->id ).'">'.lang('order_details').'</a> ';
			$acction .='<a href="'.base_url('cart/cart_a/'.$reorderQuerySreing) .'">'.lang('reorder_text').'</a> ';

		
			$response->rows[$i]['id'] = $row->id;
			$response->rows[$i++]['cell'] = array(
					"{$row->id}",
					"{$row->data_add}",
					"{$shiping->first_name} {$shiping->last_name}, {$shiping_country->name} {$shiping->city} {$shiping->address}",
					"{$status[$row->status]}",
					"{$acction}"
			);
		}
		echo  json_encode($response);
	}
	
	public function orderProductImages($orderId) {
		$this->load->model('orders_model');
		$orders = $this->orders_model->get_by_id($orderId);

		$products = json_decode($orders->products);
		$return_html = '';
		foreach ($products->content as $product){
			 $return_html .= '<div class="libraryItm left">';
			 $return_html .= '<a href="JavaScript: void(0)" rel="'.$product->id.'">';
			 $return_html .= resize_image($product->options->imageUrl,NORMAL_IMAGE_PATH,NORMAL_IMAGE_URL,'img_thumb_width','img_thumb_height',$product->name,'detailsthumb');
			 $return_html .='</a>';
			 $return_html .='<div class="checkbox" rel="'.$product->id.'"></div>
			 </div>';
		}
		echo $return_html;
	} 
	
	public function showValuationOrder($id) {
		$this->load->model('orders_model');
		$this->load->model('product_model');
		$this->load->library('parser');
		$this->load->library('exchange');
		
		$order = $this->orders_model->getAllOrders($id);
		$allProducts = json_decode($order[0]->products);
		
		$currency =  ($this->session->userdata('selected_currency') !== FALSE) ? $this->session->userdata('selected_currency') : $this->config->item('def_currency');
		
		$data= array();
		foreach ($allProducts->content as $item) {
			$product = $this->product_model->get_by_id($item->id);
			$data['image'] =  ASSETS_URL . 'catalog/small/' . $item->options->imageUrl; // resize_image( $item->options->imageUrl,NORMAL_IMAGE_PATH,NORMAL_IMAGE_URL,'list_thumbnail_width','list_thumbnail_height',$product->name,'detailsthumb');
			$data['description'] = $product->description;
			$data['value'] =lang('price'). ' - '.  $this->exchange->transformPrice($product->special_price ,$currency, true, $product->id, false, false, $product->promotion_value) . $currency;
		}
		
	
		$data['address_text'] = lang("address_text").':';
		$data['name_footer'] = lang("name_footer");
		$data['date'] = date('Y-m-d H:i:s') ;
		$data['name_text'] = lang("name").':';
		$data['document_no'] = lang('order_no') . ' #' .$id;
		$data['name'] = $order[0]->s_first_name .' '. $order[0]->s_last_name;
		$data['shipping_address'] = $order[0]->s_country . ' ' . $order[0]->s_city . ' ' .$order[0]->s_address ;
		$data['jeweller_text'] = lang("jeweller_text").":";
		$data['jeweller_name'] = $this->config->item('jeweller_name').":";
		$data['jeweller_address'] = $this->config->item("jeweller_address");
		$data['bg_image'] =  IMAGES_URL.'valutation_doc_small.jpg';
		$data['date_style'] = 'border:none; height:217px;padding-left:219px;';
		$htmlMessage =  $this->parser->parse('templates/email/valuation_document', $data, true);
		
		echo $htmlMessage;
	}
	
	public function showInvoice($id, $rate = false) {
	
		$this->load->model('orders_model');
		$this->load->model('product_model');
		$this->load->library('parser');
		$this->load->library('exchange');
		
		$currency =  ($this->session->userdata('selected_currency') !== FALSE) ? $this->session->userdata('selected_currency') :  $this->config->item('def_currency');
		
		$order = $this->orders_model->getAllOrders($id);
		$allProducts = json_decode($order[0]->products);
		$total = $allProducts->total;
		if ($rate == 1 ){
			$total = $total* $order[0]->advance_of_maney/100 ;
		} elseif ($rate == 2){
			$total = $total - ($total* $order[0]->advance_of_maney/100);
		}
		
		$data = array();
		$data['company_name'] = lang('company_name');
		$data['company_address'] = lang('company_address');
		$data['company_country'] = lang('company_country');
		$data['company_data'] = lang('company_data');
		$data['invoice_lang'] = lang('invoice');
		$data['company_vat_reg_lang'] = lang('company_vat_reg_lang');
		$data['company_vat_reg_value'] = '?';
		$data['date_lang'] = lang('date');
		$data['date'] = $order[0]->data_add;
		$data['invoice_no_lang'] = lang('invoice_number');
		$data['invoice_no'] = '#' .$id;
		$data['invoice_to'] = lang('invoice_to');
		$data['billing_address'] = $order[0]->b_first_name .' '. $order[0]->b_last_name. ', <br/>'. $order[0]->b_country . ' ' . $order[0]->b_city . ' ' .$order[0]->b_address ;
		$data['ship_to'] = lang('ship_to');
		$data['shipping_address'] = $order[0]->s_first_name .' '. $order[0]->s_last_name. ', <br/>'. $order[0]->s_country . ' ' . $order[0]->s_city . ' ' .$order[0]->s_address ;
		$data['order_number_text'] = lang('order_number');
		$data['order_number'] = '#' .$id;
		$data['item_text'] = lang('item');
		$data['description_text'] = lang('description');
		$data['amaount_text'] = lang('amaoun');
		$data['vat_text'] = lang('vat');
		$data['products'] = '';
		foreach ($allProducts->content as $item) {
			$string = '<tr style="heigth:10px">';
			$product = $this->product_model->get_by_id($item->id);
			$string .= "<td>{$product->sku}</td>" ;
			$string .= "<td> ".lang('size')." {$item->options->ring_size}</td>";
			$string .= "<td> {$item->qty}</td>";
			$string .= "<td> {$this->exchange->transformPrice($item->price , $currency, true, false, $order[0]->b_country_id, $product->promotion_value, ($order[0]->natural_person)? false : true)} $currency</td>";
			$string .= "<td> ". (($order[0]->natural_person)? $order[0]->b_country_vat : 0 ) . " %</td>";
			$string .= '</tr>';
			$data['products'] .= $string;
		}
		$data['rate'] = lang('rate');
		
		$data['currency'] = $currency;
		$data['vat'] = lang('vat');
		$data['net'] = lang('net');
		$data['rate_procent'] = $order[0]->advance_of_maney ;
		$data['vat_number'] = ($order[0]->natural_person)? $order[0]->b_country_vat : 0  . ' %';
		$data['qty_text'] = lang('qty');
		$data['total'] = lang('total');
		$data['rate_price'] =($order[0]->natural_person)? $order[0]->b_country_vat : 0  . ' %';
		$data['total_price'] = $this->exchange->transformPrice($total ,$currency, true, false, $order[0]->b_country_id, $product->promotion_value, ($order[0]->natural_person)? false : true) . " $currency" ;
		$data['subtotal'] = lang('subtotal');
		$data['vat_total'] =  lang('vat_total');
		$data['balance_due'] = lang('balance_due');
		$data['subtotal_price'] = $this->exchange->transformPrice($total , $currency, true, false, $order[0]->b_country_id, $product->promotion_value, ($order[0]->natural_person)? false : true) .  " $currency";
		$data['vat_total_summ'] =  ($order[0]->natural_person)? $order[0]->b_country_vat : 0 . ' %';
		$data['invoice_footer_text_lang'] = lang('invoice_footer_text');
		
		$htmlMessage =  $this->parser->parse('templates/email/invoice', $data, true);
		
		echo $htmlMessage;
	}
	public function logout() {
		$this->simpleloginsecure->logout();
		redirect(site_url('main'));
	}
	
	public function dashboard() {
		$this->load->model('order_address_model');
		if (!$this->access->getUserId()){
			redirect(site_url('user/create/'));
		}
		$userObj = $this->fe_user_model->get_by_id($this->access->getUserId());
		$this->load->model('orders_model');
		$user = $this->session->userdata('user');
		$orders = $this->orders_model->getLastOrder($user['id'] );
		$products = json_decode($orders[0]->products);
		
		$result_billing= $this->order_address_model->get_by_param(array('user_id' => $this->access->getUserId(), 'type' => '3'));
		if (!empty($result_billing)){
			$country = $this->country_model->get_by_id($result_billing[0]->country_id);
			$result_billing[0]->country_name = $country->name;
		}
		$data['address_billing_default'] = $result_billing[0];
		$result_shipping = $this->order_address_model->get_by_param(array('user_id' => $this->access->getUserId(), 'type' => '4'));
		if (!empty($result_shipping)){
			$country = $this->country_model->get_by_id($result_shipping[0]->country_id);
			$result_shipping[0]->country_name = $country->name;
		}
		
		$data['address_shipping_default'] = $result_shipping[0];
		
	    foreach ($products->content as $prod) {
	    	
	    	$data['prod_array'][] = array('id'=> $prod->id, 'name' => $prod->name, 'size' => $prod->options->ring_size);
	    }
		
	    $data['user_name'] = $userObj->first_name.' '. $userObj->last_name;
	    $data['user_email']= $userObj->user_email;
	    $data['user_newsletter']= $userObj->newsletter;
        $this->template->write_view ( 'content', 'front/user/dashboard.php', $data );
	      $this->template->render ();
	}
	
	public function newsLetter($flag = false) {
		if (!$this->access->getUserId()){
			redirect(site_url('user/create/'));
		}
		$userObj = $this->fe_user_model->get_by_id($this->access->getUserId());
		$data['user'] = $userObj;
		if ($flag){
			$userObj->newsletter	= htmlentities($this->input->post('newsletter'));
			$userObj->update();
			
			redirect(site_url('user/dashboard/'));
		} else {
			$this->template->write_view ( 'content', 'front/user/newsletter.php', $data );
			$this->template->render ();
		}
		
	}
	public function add_new_address($id = ''){
		$this->load->model('order_address_model');
		$this->load->helper('form');
		$this->load->library ( 'form_validation' );
		if (!$this->access->getUserId()){
			redirect(site_url('user/create/'));
		}
		$data['new_address'] = true;
		if (!empty($id)){
			$data['new_address'] = false;
			$data['address_from_db'] = $this->order_address_model->get_by_id($id);
		}
			
		$this->form_validation->set_rules ( 'first_name', lang('first_name'), 'required' );
		$this->form_validation->set_rules ( 'last_name', lang('last_name'), 'required' );
		$this->form_validation->set_rules ( 'tel_number', lang('telephone'), 'required' );
		$this->form_validation->set_rules ( 'address', lang('address'), 'required' );
		$this->form_validation->set_rules ( 'postcode', lang('zip'), 'required' );
		$this->form_validation->set_rules ( 'state_provide', lang('zip'), 'required' );
		$this->form_validation->set_rules ( 'city', lang('city'), 'required' );
		$this->form_validation->set_rules ( 'country_id', lang('country'), 'required' );
		

		if ($this->form_validation->run () == FALSE) {
			
				
			$this->template->write_view ( 'content', 'front/user/address_form_view.php', $data);
			$this->template->render ();
		}
		else {
			if($this->input->post('use_for_shipping') > 2){
				$finds = $this->order_address_model->get_by_param(array('user_id' => $this->access->getUserId(), 'type' => $this->input->post('use_for_shipping')));
				foreach ($finds as $find){
					$to_search =  ($this->input->post('use_for_shipping') == 3 ? 1 : 2);
					$this->order_address_model->fields['id'] = $find->id;
					$this->order_address_model->fields['type'] = $to_search;
					$obj = $this->order_address_model->fields_to_object();
					$obj->update();
				}
			}
			$this->order_address_model->fields['first_name'] 	= $this->input->post('first_name');
			$this->order_address_model->fields['last_name'] 	= $this->input->post('last_name');
			$this->order_address_model->fields['country_id'] 	= $this->input->post('country_id');
			$this->order_address_model->fields['company'] 		= $this->input->post('company');
			$this->order_address_model->fields['address'] 		= $this->input->post('address');
			$this->order_address_model->fields['city']    		= $this->input->post('city');
			$this->order_address_model->fields['state_provide'] = $this->input->post('state_provide');
			$this->order_address_model->fields['postcode'] 		= $this->input->post('postcode');
			$this->order_address_model->fields['tel_number'] 	= $this->input->post('tel_number');
			$this->order_address_model->fields['fax'] 			= $this->input->post('fax');
			$this->order_address_model->fields['type'] 			= $this->input->post('use_for_shipping');
			$this->order_address_model->fields['user_id'] 		= $this->input->post('last_name');
			$this->order_address_model->fields['natural_person']= $this->input->post('natural_person');
			$this->order_address_model->fields['user_id']       = $this->access->getUserId();
			$id_address = $this->input->post('address_id');
			if (isset($id_address)){
				$this->order_address_model->fields['id']       = $this->input->post('address_id');
				$billing_1 = $this->order_address_model->fields_to_object();
				$billing_1->update();
			}else {
				$billing_1 = $this->order_address_model->fields_to_object();
				$billing_1->insert();
			}
			
			redirect(site_url('user/address_book/'));
		}
		
	
	}
	
	public function address_book($id = null){
		if (!$this->access->getUserId()){
			redirect(site_url('user/create/'));
		}
		$this->load->model('order_address_model');
			
		$result_billing= $this->order_address_model->get_by_param(array('user_id' => $this->access->getUserId(), 'type' => '3'));
		if (!empty($result_billing)){
			$country = $this->country_model->get_by_id($result_billing[0]->country_id);
			$result_billing[0]->country_name = $country->name;
		}
		$data['address_billing_default'] = $result_billing[0];
		$result_shipping = $this->order_address_model->get_by_param(array('user_id' => $this->access->getUserId(), 'type' => '4'));
		if (!empty($result_shipping)){
			$country = $this->country_model->get_by_id($result_shipping[0]->country_id);
			$result_shipping[0]->country_name = $country->name;
		}
		$data['address_shipping_default'] = $result_shipping[0];
		$names = array('3', '4');
		$all = $this->order_address_model->get_by_param(array('user_id' => $this->access->getUserId(), 'type !=' => '4', 'type !=' => '3'));
		$data['address_other']= '';
		foreach ($all as $all_ass){
			$country = $this->country_model->get_by_id($all_ass->country_id);
			$all_ass->country_name = $country->name;
			$data['address_other'][] =$all_ass;
		}
		$this->template->write_view ( 'content', 'front/user/address_view.php', $data );
		$this->template->render ();
		
	}
	
	public function delete_address($id){
		if (!$this->access->getUserId()){
			redirect(site_url('user/create/'));
		}
		$this->load->model('order_address_model');
		$this->order_address_model->delete('id =' .$id);
		redirect(site_url('user/address_book/'));
	}
	
	public function forgotPassword(){
		$this->load->helper('form');
		$this->load->library('parser');
		$this->load->library('email');
		
		$email = ($this->input->post('fgpassemail')) ? $this->input->post('fgpassemail') : '';
		
		if (empty($email)){
			$this->template->write_view ( 'content', 'front/user/forgot_password.php', $data );
			$this->template->render ();
		} else {
			$userObj = $this->fe_user_model->get_by_param(array('user_email'=>$email ));
			
			$characters = 'abcdefghijklmnopqrstuvwxyz';
			$characters .= strtoupper('abcdefghijklmnopqrstuvwxyz');
			$characters .= '0123456789';
			$string = '';
			for ($p = 0; $p < 6; $p++) {
				$string .= $characters[mt_rand(0, strlen($characters))];
			}
			
			$data['logo'] = base_url('/application/assets/images/logo.png');
			$data['hello_name'] = lang('helli').', '. $userObj[0]->first_name.' '.$userObj[0]->last_name;
			$data['your_password'] = lang('your_password_is') .' '. $string;
			$data['forgot_pass_email_text'] = lang('forgot_pass_email_text');
			$data['thank_you_forgot_email_text'] = lang('thank_you_forgot_email_text');
			
			$htmlContent = $this->parser->parse('templates/email/forgot_pass', $data);

			$hasher = new PasswordHash(8, false);

			$userObj = $this->fe_user_model->get_by_id($userObj[0]->id);
			$userObj->user_pass = $hasher->HashPassword($string);
	
			$userObj->update();
				
			$this->email->initialize(array('mailtype' => 'html', 'protocol' => 'sendmail'));
			
			$this->email->from($this->config->item('contact_email_address'), $this->config->item('office_name') );
	        $this->email->to($email,$name);
	        $this->email->mailtype = 'html';
	        $this->email->subject(lang('forgott_pass'));
	        $this->email->message($htmlContent);
	    
			if ($this->email->send()){
				redirect(site_url('user/create/'));
			} else {
				redirect(site_url('user/create/'));
			}
			$this->email->print_debugger();
		}
	}
}